package com.ebeitech.doorapp.view.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebeitech.doorapp.R;
import com.ebeitech.doorapp.http.EbeiErrorCode;
import com.ebeitech.doorapp.http.HttpListener;
import com.ebeitech.doorapp.http.HttpService;
import com.ebeitech.doorapp.http.model.BaseModel;
import com.ebeitech.doorapp.http.service.HttpCommonServiceRx;
import com.ebeitech.doorapp.view.BaseActivity;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.ui.CommonItemView;
import com.ebeitech.library.ui.CommonTitleBar;
import com.ebeitech.library.ui.dialog.CommonAlertDialogFragment;
import com.ebeitech.library.util.CommonUtil;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by alice on 2018/6/12.
 */

public class ChangePasswordActivity extends BaseActivity {

	@BindView(R.id.view_title)
	CommonTitleBar mTitleView;
	@BindView(R.id.topParentView)
	LinearLayout mTopParentView;
	CommonItemView mOldPasswordView, mNewPasswordView, mConfirmView;

	private Context mContext;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.ebei_activity_listview);
		ButterKnife.bind(this);
		mContext = this;
		initView();
	}

	private void initView() {
		mTitleView.setTitle(getString(R.string.ebei_change_password));

		List<CommonItemView> list = new ArrayList<CommonItemView>();
		mOldPasswordView = new CommonItemView(mContext);
		mOldPasswordView.setTitle(getString(R.string.ebei_old_password));
		mOldPasswordView.setEditHint(getString(R.string.ebei_input_hint_x, getString(R.string.ebei_old_password)));
		mOldPasswordView.setItemStyle(CommonItemView.ItemStyle.EDIT);
		mOldPasswordView.setEditForPasswordType();
		mOldPasswordView.setIsHeaderViewVisible(true);
		list.add(mOldPasswordView);
		mNewPasswordView = new CommonItemView(mContext);
		mNewPasswordView.setTitle(getString(R.string.ebei_new_password));
		mNewPasswordView.setEditHint(getString(R.string.ebei_input_hint_x, getString(R.string.ebei_new_password)));
		mNewPasswordView.setItemStyle(CommonItemView.ItemStyle.EDIT);
		mNewPasswordView.setEditForPasswordType();
		list.add(mNewPasswordView);
		mConfirmView = new CommonItemView(mContext);
		mConfirmView.setTitle(getString(R.string.ebei_confirm_password));
		mConfirmView.setEditHint(getString(R.string.ebei_input_hint_x, getString(R.string.ebei_new_password)));
		mConfirmView.setItemStyle(CommonItemView.ItemStyle.EDIT);
		mConfirmView.setEditForPasswordType();
		list.add(mConfirmView);

		for (int i = 0; i < list.size(); i++) {
			CommonItemView commonItemView = list.get(i);
			if (i == list.size() - 1) {
				commonItemView.setIsDividerViewVisible(false);
			}
			mTopParentView.addView(commonItemView);
		}

		LinearLayout view = new LinearLayout(mContext);
		int margin = ViewUtil.dp2px(mContext, 20);
		view.setPadding(margin, (int) getResources().getDimension(R.dimen.ebei_button_bottom_margin), margin,  0);
		TextView submitButton = new TextView(mContext);
		submitButton.setTextAppearance(mContext, R.style.EbeiBottomButtonBlue);
		submitButton.setText(R.string.ebei_submit);
		submitButton.setBackgroundResource(R.drawable.ebei_corner_bg_blue);
		submitButton.setGravity(Gravity.CENTER);
		margin = ViewUtil.dp2px(mContext, 10);
		submitButton.setPadding(0, margin, 0, margin);
		submitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changePassword();
			}
		});
		view.addView(submitButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		mTopParentView.addView(view);
	}

	private void changePassword() {
		if (!CommonUtil.isNetworkAvailable(mContext)) {
			ViewUtil.toastMsg(mContext, R.string.ebei_network_disconnect);
			return;
		}

		final String oldPassword = mOldPasswordView.getEditValue();
		final String newPassword = mNewPasswordView.getEditValue();
		if (StringUtil.isStringNullOrEmpty(newPassword)) {
			ViewUtil.toastMsg(mContext, getString(R.string.ebei_x_not_null, getString(R.string.ebei_new_password)));
			return;
		}
		if (newPassword.length() < 6 || StringUtil.isNumberStr(newPassword)) {
			ViewUtil.toastMsg(mContext, getString(R.string.ebei_password_not_valid));
			return;
		}

		String confirmPassword = mConfirmView.getEditValue();
		if (!newPassword.equals(confirmPassword)) {
			ViewUtil.toastMsg(mContext, getString(R.string.ebei_password_inconsitent));
			return;
		}

		HttpCommonServiceRx CommonServiceRx = HttpService.getInstance().getRxGsonRetrofit().create(HttpCommonServiceRx.class);
		Map<String, String> params = new HashMap<String, String>();
		params.put("oldPassword", oldPassword);
		params.put("newPassword", newPassword);
		String url = "platform/user/pwd/" + SPManager.getInstance(mContext).get(SPConstants.USER_ID, "");
		Call<ResponseBody> call = CommonServiceRx.changePassword(url, params);
		final CommonAlertDialogFragment dialogFragment = ViewUtil.showProgressDialog(mContext);
		HttpService.getInstance().executeHttp(mContext, call, new HttpListener<BaseModel>() {
			@Override
			public void onStart() {
			}

			@Override
			public void onError(EbeiErrorCode errorCode) {
				ViewUtil.dismissDialog(dialogFragment);
				String errorMsg = errorCode.getErrorMsg();
				if (StringUtil.isStringNullOrEmpty(errorMsg)) {
					errorMsg = getString(R.string.ebei_modify_password_failure);
				}
				ViewUtil.toastMsg(mContext, errorMsg);
			}

			@Override
			public void onSuccess(BaseModel baseModel) {
				ViewUtil.dismissDialog(dialogFragment);
				ViewUtil.toastMsg(mContext, getString(R.string.ebei_modify_password_success));
				finish();
			}

		}, BaseModel.class, false);
	}
}
